// import log from '@dfeidao/atom-nodejs/logger/log';
import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	plan_type_no: string;
	plan_cla_attr_no: string;
	pn: number;
	plan_list_name: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	// log('Service begin path:c:\Users\32645\Desktop\kh\src\plan-cb\zj-000006\s001,action_id:' + action_id);

	// log('Service end path:c:\Users\32645\Desktop\kh\src\plan-cb\zj-000006\s001,action_id:' + action_id);

	const q = query(action_id, session_id, systemid);
	q.prepare('plan_list', ['plan_list_no', 'plan_list_name', 'plan_list_depict'], { productid, plan_type_no: message.plan_type_no, plan_cla_attr_no: message.plan_cla_attr_no, plan_list_name: { $like: '%' + message.plan_list_name + '%' } }, 12, message.pn, [], []);
	const [d] = await q.exec();

	const { prepare: p, exec } = count(action_id, session_id, systemid);
	p('plan_list', { productid, plan_type_no: message.plan_type_no, plan_cla_attr_no: message.plan_cla_attr_no, plan_list_name: { $like: '%' + message.plan_list_name + '%' } });
	const [c] = await exec();

	const res = d.map((value) => {
		return { ...value, v: JSON.stringify(value) };
	});

	return {
		data: { res, c }
	};
}
